/*****************************************************************************//*!
* file  : test.c
* creat : apleilx
* data  : 2017/03/02
*******************************************************************************/
#include "osObjects.h"                                          // RTOS object definitions
#include "lcd_eval.h"
#include "touch_panel.h"
#include "picture.h"
#include "guitask.h"

uint16_t a1=20;
uint16_t a2=120;
uint16_t a3=220;
uint16_t b1=10;
uint16_t b2=100;
uint16_t b3=200;
uint16_t device_code;
uint8_t error_string[]="Please press the button!";
char_format_struct char_format;
uint8_t led_string[4][4]= {"LED2","LED3","LED4","LED5"} ;
uint16_t num[4]= {0,0,0,0}; /* store the touch point number on four buttons */
uint8_t count=0;

uint8_t button_id=0;
extern uint16_t touch_ad_x,touch_ad_y;
void get_touch_area(uint16_t x,uint16_t y,uint16_t *num_array);
uint8_t find_max(uint16_t* num_array);
void change_picture(uint8_t button_id);

/*----------------------------------------------------------------------------
 *                             Gui task
 *---------------------------------------------------------------------------*/
void GuiTask (void   *argument)
{
    uint16_t i;
    rt_uint32_t mail_val;
    
    exmc_lcd_init();

    osDelay(ms_ticks(50));
    
    /* initialize the LCD */
    lcd_init();
    lcd_clear(LCD_COLOR_WHITE);
    lcd_picture_draw(40,200,40+160-1,200+87-1,(uint16_t *)(picture + BMP_HEADSIZE));
    lcd_rectangle_draw(10,10,230,310,LCD_COLOR_BLUE);
    /* configure char format */
    char_format.char_color = LCD_COLOR_BLUE;
    char_format.bk_color = LCD_COLOR_WHITE;
    char_format.direction = CHAR_DIRECTION_VERTICAL;
    char_format.font = CHAR_FONT_8_16;
    /* draw character on LCD screen */
    for (i = 0; i < 4; i++)
    {
        lcd_char_display((a1+35+8*i), b1+20, *(led_string[0]+i), char_format);
        lcd_char_display((a2+35+8*i), b1+20, *(led_string[1]+i), char_format);
        lcd_char_display((a1+35+8*i), b2+20, *(led_string[2]+i), char_format);
        lcd_char_display((a2+35+8*i), b2+20, *(led_string[3]+i), char_format);
    }

    /* draw picture of button on LCD screen */
    lcd_picture_draw(a1+30,b1+40,a1+30+40-1,b1+40+40-1,(uint16_t *)(image_off + BMP_HEADSIZE));
    lcd_picture_draw(a2+30,b1+40,a2+30+40-1,b1+40+40-1,(uint16_t *)(image_off + BMP_HEADSIZE));
    lcd_picture_draw(a1+30,b2+40,a1+30+40-1,b2+40+40-1,(uint16_t *)(image_off + BMP_HEADSIZE));
    lcd_picture_draw(a2+30,b2+40,a2+30+40-1,b2+40+40-1,(uint16_t *)(image_off + BMP_HEADSIZE));


    for(;;)
    {
        if(RT_EOK == osMailGet(qbid_gui,&mail_val,ms_ticks(500)))
        {
            switch(mail_val)
            {
                case GUI_MB_TOUCH_ACT:
                    count++;
                    get_touch_area(touch_coordinate_x_get(touch_ad_x),(LCD_Y - touch_coordinate_y_get(touch_ad_y)),num);
                    if(count==20)
                    {
                        button_id = find_max(num);

                        change_picture(button_id);
                        num[0]=num[1]=num[2]=num[3]=0;
                        count=0;
                    }                
                    break;
                default:
                    break;
            }
        }
        /*  generate response to the touch(turn on LED and change picture )*/
    }
}

/*!
    \brief      get the touch area
    \param[in]  x: the row-coordinate
    \param[in]  y: the column-coordinate
    \param[out] num_array: touch area array pointer
    \retval     none
*/
void get_touch_area(uint16_t x,uint16_t y,uint16_t *num_array)
{
    if((x<a1+70)&&(x>a1+30)&&(y>b1+30)&&(y<b1+70))
    {
        num_array[0]++;
    }
    else if((x<a2+70)&&(x>a2+30)&&(y>b1+30)&&(y<b1+70))
    {
        num_array[1]++;
    }
    else if((x<a1+70)&&(x>a1+30)&&(y>b2+30)&&(y<b2+70))
    {
        num_array[2]++;
    }
    else if((x<a2+70)&&(x>a2+30)&&(y>b2+30)&&(y<b2+70))
    {
        num_array[3]++;
    }
}

/*!
    \brief      find the position of max value in array
    \param[in]  none
    \param[out] none
    \retval     the position of max value in num_array array
*/
uint8_t find_max(uint16_t* num_array)
{
    int id;
    id=0;
    if(num_array[id]<num_array[1])
    {
        id=1;
    }
    if(num_array[id]<num_array[2])
    {
        id=2;
    }
    if(num_array[id]<num_array[3])
    {
        id=3;
    }
    if(num_array[id]==0)
    {
        id=4;
    }
    return id;
}

/*!
    \brief      change the picture of LED
    \param[in]  button_id: the id of button you just pressed
    \param[out] none
    \retval     none
*/
void change_picture(uint8_t button_id)
{
    uint8_t len_s;
    uint8_t *str;
    uint16_t i;

    /* draw picture on LCD screen */
    lcd_picture_draw(a1+30,b1+40,a1+30+40-1,b1+40+40-1,(uint16_t *)(image_off + BMP_HEADSIZE));
    lcd_picture_draw(a2+30,b1+40,a2+30+40-1,b1+40+40-1,(uint16_t *)(image_off + BMP_HEADSIZE));
    lcd_picture_draw(a1+30,b2+40,a1+30+40-1,b2+40+40-1,(uint16_t *)(image_off + BMP_HEADSIZE));
    lcd_picture_draw(a2+30,b2+40,a2+30+40-1,b2+40+40-1,(uint16_t *)(image_off + BMP_HEADSIZE));
    /* draw charater on LCD screen */
    len_s=sizeof(error_string)-1;
    str = error_string;

    char_format.char_color = LCD_COLOR_WHITE;
    for (i = 0; i < len_s; i++)
    {
        lcd_char_display((30+8*i), 190, *str++, char_format);
    }

    switch(button_id)
    {
    case 0:
        lcd_picture_draw(a1+30,b1+40,a1+30+40-1,b1+40+40-1,(uint16_t *)(image_on + BMP_HEADSIZE));
        break;
    case 1:
        lcd_picture_draw(a2+30,b1+40,a2+30+40-1,b1+40+40-1,(uint16_t *)(image_on + BMP_HEADSIZE));
        break;
    case 2:
        lcd_picture_draw(a1+30,b2+40,a1+30+40-1,b2+40+40-1,(uint16_t *)(image_on + BMP_HEADSIZE));
        break;
    case 3:
        lcd_picture_draw(a2+30,b2+40,a2+30+40-1,b2+40+40-1,(uint16_t *)(image_on + BMP_HEADSIZE));
        break;
    default:
        str = error_string;
        char_format.char_color = LCD_COLOR_RED;
        for (i = 0; i < len_s; i++)
        {
            lcd_char_display((30+8*i), 190, *str++, char_format);
        }
        break;
    }
}
